'==============================================================================
' LANG		: VBScript 
' NAME		: FCS-SampleScript Install FCS Client.vbs 
' VERSION	: 2.0000 12/3/2007
' Description 	: Script to install FCS client components   
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'
' Copyright (C) 2007.  Microsoft Corporation.  All rights reserved.
'
' NOTES		: ALL SAMPLE SCRIPTS BEGIN WITH THE SAME SETUP ELEMENTS
'                 You can use this script standalone or incoporate the 
'                 subroutines in this script into a larger script.
'==============================================================================
Option Explicit
Dim bDEBUG : bDEBUG=False 'Set to TRUE to help with script debugging.
' Constants Declarations
  Const WAIT_ON_RETURN 	      = True
  Const DO_NOT_WAIT_ON_RETURN = False
  Const HIDE_WINDOW           = 0
  Const SHOW_WINDOW           = 1
  Const ThisComputer          = "."
'==============================================================================
' Main Routine
  On Error Resume Next
  If bDEBUG then On Error Goto 0
  
' Create system objects
  Dim WshShell      : Set WshShell = WScript.CreateObject("WScript.Shell")
  Dim objRegistry   : Set objRegistry = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" &_
			                            ThisComputer & "\root\default:StdRegProv")
  Dim objWMIService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
                                                     ThisComputer & "\root\cimv2")

' Call the installation functions
  'Call FCS_InstallXPSP2Hotfix
  'Call UninstallMcAfee, Trend, Symantec, Sophos, eTrust...
  'Call UninstallAntiSpywareProducts
  Call InstallFCSAgents
  
' Clean up objects and exit
  Set objWMIService = Nothing
  Set objRegistry = Nothing
  Set WshShell = Nothing
  
  wscript.quit
  
'=====================================================================================
' SubRoutine InstallFCSAgents
'       This routine installs the FCS client agents and triggers policy and signature
'       file updates
'       
'  INPUTS: None
'  OUTPUT: None (except error messages)
' ASSUMES: The existence of the WshShell, objRegistry and objWMIService objects
'          Executables are in the directory the script was executed from or 
'          in the file search PATH
'	   YOU HAVE SET CollectionServerName to the name of your collection server 	   
'
'   NOTES: This routine uses the default values for setup
'            /CG MOMManagementGroup - The default name is ForefrontClientSecurity
'            /MS CollectionServer - FQDN of the FQDN collection server
'            /I InstallationFolder - (optional) install folder
'            /L LoggingFolder - (optional) folder for client installation logs
'            /NOMOM - (optional) Don't installs the MOM agent
'          You may choose to modify the installation command to add optional parameters
'======================================================================================
Sub InstallFCSAgents
  
    Const CollectionServerName = "FQDNYourCollectionServer"
  
    If bDEBUG Then wscript.echo "DEBUG: Installing FCS client with MOM Agent"
    RetVal = WshShell.Run("clientsetup.exe /CG forefrontclientsecurity /MS " & CollectionServerName,HIDE_WINDOW,WAIT_ON_RETURN)
  
    If bDEBUG and RetVal Then wscript.echo "DEBUG: Client Setup return code was " & CStr(RetVal) 
    
  ' Check for installed FCS Policies
    Dim PoliciesAreApplied : PoliciesAreApplied = False
    Const PolicyRegKey = "SOFTWARE\Policies\Microsoft"
    Const HKEY_LOCAL_MACHINE = &H80000002

    Dim SubKeyArray, SubKey
    objRegistry.EnumKey HKEY_LOCAL_MACHINE, PolicyRegKey, SubKeyArray
    For Each SubKey In SubKeyArray
	If SubKey = "Microsoft Forefront" Then
		PoliciesAreApplied = True
	End If 
    Next 
    
    If PoliciesAreApplied = False Then
    If bDEBUG Then wscript.echo "DEBUG: Updating FCS policies"
    	WshShell.Run "fcslocalpolicytool.exe /f /i FCSPolGeneral.reg",HIDE_WINDOW,WAIT_ON_RETURN

	' As an alternative you can run the gpupdate command on XP and 2003 machines
	' or the secedit command for W2K machines
	'WshShell.Run "gpupdate /force /TARGET:Computer",HIDE_WINDOW,WAIT_ON_RETURN
	'WshShell.Run "secedit /refreshpolicy machine_policy /enforce",HIDE_WINDOW,WAIT_ON_RETURN
		
    End If

  ' Pause 30 seconds so installation and policy update can complete
    WScript.Sleep(30000)

  ' Verify the FCS agent is running
    If bDEBUG Then wscript.echo "DEBUG: Verifying FCS client is running."
    Dim ServicesList, ServiceObj, ServiceIsRunning : ServiceIsRunning = False
    Set ServicesList = objWMIService.ExecQuery("Select * from Win32_Service WHERE Name='FCSAM'")
    
    For Each ServiceObj In ServicesList 
    	If ServiceObj.State = "Running" then
    		ServiceIsRunning = True
	End if
    Next
    If NOT ServiceIsRunning Then
	MsgBox "There was an error installing Antivirus software on your computer.  Please contact the Help Desk", ,"Software Installation Error"
	WScript.Quit(2)
    End if
    
  ' Trigger signature file update on client
    If bDEBUG Then wscript.echo "DEBUG: Trigging signature file updates."
    WshShell.Run "wuauclt.exe /detectnow",HIDE_WINDOW,WAIT_ON_RETURN
    If bDEBUG Then On Error Goto 0

End Sub